#include "multiSprite.h"

#include "clock.h"
#include "viewport.h"

using std::cout; using std::endl;

void MultiSprite::advanceFrame(Uint32) {
	if(velocityX() > 0.5) {
		static double ms = static_cast<float>(frameInterval)/1000.0;
		Uint32 ticks = Clock::getInstance().getTicks();
		int df = ticks * ms;
		currentFrame = (df % numberOfFrames) + offset;
	}
	else if(velocityX() < -0.5) {
		static double ms = static_cast<float>(frameInterval)/1000.0;
		Uint32 ticks = Clock::getInstance().getTicks();
		int df = ticks * ms;
		currentFrame = (numberOfFrames - (df % numberOfFrames) - 1) + offset;
	}
}

void MultiSprite::update(Uint32 ticks) {
	Vector2f max = getMaxVelocity();
	//if(velocityX() > max[0]) velocityX(max[0]);
	//if(velocityY() > max[1]) velocityY(max[1]);

	advanceFrame(ticks);
}

void MultiSprite::handleMouseMotion()
{
	int x, y;
	SDL_GetMouseState(&x, &y);
	if((static_cast<int>(x + Viewport::getInstance().X())) > static_cast<int>((X() + frame->getWidth() / 2))) {
		if(currentFrame >= static_cast<unsigned int>(Gamedata::getInstance().getXmlInt(name+"FramesCol"))) {
			currentFrame = currentFrame - Gamedata::getInstance().getXmlInt(name+"FramesCol");
		}
		offset = 0;
	} else {
		if(currentFrame < static_cast<unsigned int>(Gamedata::getInstance().getXmlInt(name+"FramesCol"))) {
			currentFrame = currentFrame + Gamedata::getInstance().getXmlInt(name+"FramesCol");
		}
		offset = Gamedata::getInstance().getXmlInt(name+"FramesCol");
	}
}
